<?php
/**
 * Created by PhpStorm.
 * User: 董伟
 * Date: 2019/10/31
 * Time: 11:50
 */

namespace app\common\logic;
use app\facade\ShopType as ShopTypeFacade;
use app\common\model\ShopType as ShopTypeModel;


class ShopType extends Base
{
    /**
     * 重组提交信息
     * @param  array $data 请求的信息
     */
    public function recombine($data)
    {
        $data['image_id']          = isset($data['image_id']) && $data['image_id'] ? (int)$data['image_id'] : 0;
        $data['ad_id']             = isset($data['ad_id']) && $data['ad_id'] ? (int)$data['ad_id'] : 0;
        $data['is_home_recommand'] = isset($data['is_home_recommand']) && $data['is_home_recommand'] ? $data['is_home_recommand'] : 0;
        $data['is_show']           = isset($data['is_show']) && $data['is_show'] ? $data['is_show'] : 0;
        return $data;
    }

    /**
     * 添加/编辑逻辑处理
     * @param  array $data 请求的信息
     */
    public function save($data)
    {
        if (isset($data['id']) && $data['id']) {
            // 编辑
            if ($data['id'] == $data['parent_id']) {
                exception('不能选择自己作为父级分类');
            }
            $sub_ids = get_sub_ids('app\common\model\GoodCategory', $data['id']);
            if (in_array($data['parent_id'], $sub_ids)) {
                exception('要修改的父级分类是当前分类的子分类');
            }
        }
        $result = parent::save($data);
        if ($result !== true && $result) {
            // 添加
            $data['id'] = $result;
        }
        $this->resetCategoriesLevel($data);
        return $result;
    }

    /**
     * 重置栏目的父级栏目自己栏目层级关系
     * @param array $data 请求的信息
     */
    private function resetCategoriesLevel($data)
    {
        $model    = '\app\common\model\ShopType';
        $superIds = array_reverse(get_super_ids($model, $data['id']));
        $subIds   = ShopTypeFacade::getLastCategories(get_sub_ids($model, $data['id']));

        $ids[]    = $data['id'];
        // 当前栏目原始父级栏目 id
        if (isset($data['origin_super_category_ids']) && $data['origin_super_category_ids']) {
            $ids = array_merge($ids, explode(',', $data['origin_super_category_ids']));
        }

        // 当前栏目原始子级栏目 id
        if (isset($data['origin_sub_category_ids']) && $data['origin_sub_category_ids']) {
            $ids = array_merge($ids, explode(',', $data['origin_sub_category_ids']));
        }

        // 当前栏目现在的所有父级栏目 id
        if (!empty($superIds)) {
            foreach ($superIds as $v) {
                $ids[] = $v;
            }
        }

        // 当前栏目现在的所有子级栏目 id
        if (!empty($subIds)) {
            foreach ($subIds as $v) {
                $ids[] = $v;
            }
        }

        $ids = array_unique($ids);
        if (!empty($ids)) {
            $update = [];
            foreach ($ids as $v) {
                $temp_super_ids = array_reverse(get_super_ids($model, $v));
                $temp_sub_ids   = ShopTypeFacade::getLastCategories(get_sub_ids($model, $v));
                $update[] = [
                    'id'               => $v,
                    'super_category_ids' => !empty($temp_super_ids) ? implode(',', $temp_super_ids) : 0,
                    'sub_category_ids'   => !empty($temp_sub_ids) ? implode(',', $temp_sub_ids) : 0,
                ];
            }
            (new ShopTypeModel)->saveAll($update);
        }
    }

}